<?php
/*
Plugin Name: Shuffle
Plugin URI: http://wordpress.org/extend/plugins/shuffle/
Description: Shuffle the contents.
Author: fnsoxt at gmail.com
Version: 0.5
Author URI: http://hi.baidu.com/fnsoxt
 */

function shuffle_js(){
    echo <<<EOF
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p.shuffle").each(function(i,item){
var list = new Array();
$(item).children('span').each(function(j,obj){ num = $(obj).attr('class'); list[num] = obj; });
$(item).empty(); for(var k=0;k<list.length;k++){ $(item).append(list[k]); }
}); });
</script>
EOF;
}
function shuffle_content($content) {
    $seed ='fnsoxt';$start=5;$length=10;
    srand(crc32($seed));
    $c_a = explode("\n",trim(strip_tags($content,'<img><a>')));
    foreach($c_a as $k => &$v){
        $c_b = explode("。",trim($v,"。"));
        foreach($c_b as $key => &$value)
            $value = sprintf('<span class="%d">%s。</span>'.mb_substr($value,$start,$length),$key,$value);
        shuffle($c_b);
        $v = '<p class="shuffle">'.implode('',$c_b).'</p>';
    }
    return implode("\n",$c_a);
}

add_filter( 'wp_head', 'Shuffle_js' );
add_filter( 'the_content', 'Shuffle_content' );
